<template>
    <div class="continer" v-loading="upimgLoding">
        <!-- 搜索 -->
        <div class="topbox">
            <div>
                <el-button type="primary" @click="openWord">发布活动</el-button>
                <el-button icon="el-icon-refresh" @click="getList">刷新列表</el-button>
            </div>
            <el-form :model="search" :inline="true" ref="sc" @submit.native.prevent>
                <el-form-item label="" prop="title">
                    <el-input type="text" v-model="search.title" placeholder="请输入活动标题"></el-input>
                </el-form-item>
                <el-form-item label="" prop="sponsor">
                    <el-input type="text" v-model="search.sponsor" placeholder="请输入主办方"></el-input>
                </el-form-item>
                <el-button type="primary" class="ml_10" @click="()=>{getList()}" native-type="submit">搜索</el-button>
                <el-button @click="resetForm('sc')">重置</el-button>
            </el-form>
        </div>
        <!-- 列表 -->
        <el-table v-loading="tableload" :data="listData" row-key="id" style="width:100%" default-expand-all stripe border>
            <el-table-column label="活动主题" prop="title"></el-table-column>
            <el-table-column label="主办方" prop="sponsor"></el-table-column>
            <el-table-column label="活动地址" prop="addr"></el-table-column>
            <el-table-column label="操作">
                <template slot-scope="scope">
                    <el-button type="text" size="mini" @click="()=>{detail(scope.row)}">查看</el-button>
                    <el-button type="text" size="mini" @click="()=>{del(scope.row.id)}">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-pagination
            background
            layout="prev, pager, next, jumper"
            :total="listLen"
            :page-size="limit"
            @current-change="toPage"
            :current-page.sync="currentPage"
            class="txt_r mt_20"
        ></el-pagination>
        <!-- 发布 -->
        <el-dialog :title="word.formTitle" :visible.sync="showForm" width="1100px">
            <el-form
              :model="word"
              label-position="right"
              :rules="formRules"
              ref="word"
              label-width="80px"
              >
                <el-form-item label="封面图" prop="discover">
                    <el-upload
                        :action="BASE_URL+'/admin/file/upload/createfile'"
                        name="file"
                        :before-upload="upimgBefore"
                        :on-success="upimgSuccess"
                        :on-error="upimgError"
                        :show-file-list="false"
                    >
                        <el-button v-if="!word.discover" size="small" type="primary" ref="btn">点击上传</el-button>
                        <img v-else :src="BASE_URL + '/' + word.discover" class="form-img" />
                    </el-upload>
                </el-form-item>
                <el-form-item label="活动主题" prop="title">
                    <el-input type="text" v-model="word.title" placeholder="请输入活动主题"></el-input>
                </el-form-item>
                <el-form-item label="主办方" prop="sponsor">
                    <el-input type="text" v-model="word.sponsor" placeholder="请输入主办方"></el-input>
                </el-form-item>
                <el-form-item label="活动地址" prop="addr">
                    <el-input type="text" v-model="word.addr" placeholder="请输入活动地址"></el-input>
                </el-form-item>
                <el-form-item label="日期时间" prop="riqi">
                    <el-date-picker
                          v-model="word.riqi"
                          type="datetimerange"
                          align="right"
                          start-placeholder="开始日期"
                          end-placeholder="结束日期"
                          :default-time="['00:00:00', '23:59:59']"
                          value-format='yyyy-MM-dd HH:mm:ss'>
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="活动内容" prop="content">
                    <vue-ueditor-wrap v-model="word.content" :config="ueConfig"></vue-ueditor-wrap>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="showForm = false">取 消</el-button>
                <el-button type="primary" @click="addWord">发 布</el-button>
            </div>
        </el-dialog>
        <!-- 查看 -->
        <el-dialog title="详情" :visible.sync="showDetail" width="85%">
            <div class="word">
                <div>
                    <div style="width: 100px; float: left;">活动主题：</div>
                    <div>{{detailData.title}}</div>
                </div>
                <div>
                    <div style="width: 100px; float: left;">主办方：</div>
                    <div>{{detailData.sponsor}}</div>
                </div>
                <div>
                    <div style="width: 100px; float: left;">活动地址：</div>
                    <div>{{detailData.addr}}</div>
                </div>
                <div>
                    <div style="width: 100px; float: left;">开始时间：</div>
                    <div>{{detailData.start_time}}</div>
                </div>
                <div>
                    <div style="width: 100px; float: left;">结束时间：</div>
                    <div>{{detailData.end_time}}</div>
                </div>
                <div>
                    <div style="width: 100px; float: left;">封面：</div>
                    <div>
                        <img style="width: 300px;" :src="BASE_URL + '/' + detailData.discover" >
                    </div>
                </div>
                <div>
                    <div style="width: 100px; float: left;">活动内容：</div>
                    <div style="margin-left: 20px;" v-html="detailData.content"></div>
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { BASE_URL } from "../../config/app";
import * as apis from "../../api/huodong/huodong.js";
import VueUeditorWrap from 'vue-ueditor-wrap';
export default {
    data() {
        return {
            BASE_URL: '',
            upimgLoding: false,
            // 搜索
            search: {
                title: "",
                sponsor: "",
                id: ""
            },
            // 列表
            tableload:false,
            listData:[],
            // 分页
            listLen: 0,
            limit: 14,
            currentPage: 1,
            // 发布
            showForm: false,
            word:{
                formTitle: "发布活动",
                discover: "",
                title: "",
                sponsor: "",
                addr: "",
                content: "",
                riqi: "",
            },
            formRules: {
                title: [{required: true,message: "请输入标题",trigger: "blur"}],
                sponsor: [{required: true,message: "请输入主办方",trigger: "blur"}],
                addr: [{required: true,message: "请输入活动地址",trigger: "change" }],
                content: [{required: true,message: "请输入活动内容",trigger: "blur"}],
                riqi: [{required: true,message: "请输入开始时间",trigger: "blur"}],
            },
            // 百度编辑器
            ueditorMsg: '',
            ueConfig: {
                // 初始容器高度
                initialFrameHeight: 300,
                // 初始容器宽度
                initialFrameWidth: '100%',
                // 上传文件接口
                elementPathEnable: false,
                wordCount: false,
                UEDITOR_HOME_URL: '/UEditor/',
                imageActionName: 'uploadimage', //执行上传图片的action名称,
                imageFieldName: "file", //提交的图片表单名称
                imageAllowFiles: [".png", ".jpg", ".jpeg", ".gif", ".bmp"],
                "fileActionName": "uploadfile",
                "fileFieldName": "file",
                "fileAllowFiles": [
                    ".png", ".jpg", ".jpeg", ".gif", ".bmp",
                    ".flv", ".swf", ".mkv", ".avi", ".rm", ".rmvb", ".mpeg", ".mpg",
                    ".ogg", ".ogv", ".mov", ".wmv", ".mp4", ".webm", ".mp3", ".wav", ".mid",
                    ".rar", ".zip", ".tar", ".gz", ".7z", ".bz2", ".cab", ".iso",
                    ".doc", ".docx", ".xls", ".xlsx", ".ppt", ".pptx", ".pdf", ".txt", ".md", ".xml"
                ],
            },
            // 查看
            showDetail: false,
            detailData: {}
        };
    },
    created(){
        this.BASE_URL = BASE_URL;
        this.getList();
    },
    methods: {
        getList(page=1){
            this.tableload = true;
            apis.getinfo({
                ...this.search,
                page: page,
                per_page: this.limit
            }).then((data)=>{
                this.tableload = false;
                if(data.code){
                    this.$message.error(data.message);
                    return;
                }
                this.listLen = data.data.total;
                this.limit = parseInt(data.data.per_page);
                this.listData = data.data.list;
            }).catch(()=>{
                this.tableload = false;
                this.$message({type: "error",message: "请求失败",duration: 1800});
            });
        },
        // 发布
        openWord(){ // 发布按钮
            this.word = {
                formTitle: "发布活动",
                discover: "",
                title: "",
                sponsor: "",
                addr: "",
                content: "",
                riqi: ''
            };
            this.showForm = true;
        },
        addWord(){ // 添加
            this.$refs['word'].validate((valid) => {
                if (!valid) {return false;}
                apis.addinfo({
                    title: this.word.title,
                    content: this.word.content,
                    discover: this.word.discover,
                    start_time: this.word.riqi[0],
                    end_time: this.word.riqi[1],
                    addr: this.word.addr,
                    sponsor: this.word.sponsor
                }).then((data)=>{
                    if(data.code){
                        this.$message.error(data.message);
                        return false;
                    }
                    this.$message.success("发布成功");
                    this.showForm = false;
                    this.getList();
                }).catch(()=>{
                    this.$message.error("发布失败");
                })
            });
        },
        // 清空重置
        resetForm(name){
            this.$refs[name].resetFields();
        },
        // 分页
        toPage(val){
            this.currentPage = val;
            this.getList(val);
        },
        // 插入图片
        upimgBefore(){ this.upimgLoding = true; },
        upimgSuccess(data){
            this.upimgLoding = false;
            this.word.discover = data.data.key;
        },
        upimgError(){
            this.upimgLoding = false;
            this.$message.error('图片插入失败');
        },
        // 删除
        del(id){
            this.$confirm("确定删除该活动吗", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(()=>{
                apis.del({id:id}).then((data)=>{
                    if(data.code){
                        this.$message.error(data.message);
                        return;
                    }
                    this.$message.success("删除成功");
                    this.getList(this.currentPage);
                }).catch(()=>{
                    this.$message.error("删除失败");
                })
            }).catch(()=>{});
        },
        // 查看
        detail(row){
            this.detailData = row;
            var regex1 = new RegExp("(i?)(\<img)(?!(.*?style=['\"](.*)['\"])[^\>]+\>)","gmi");
            this.detailData.content = this.detailData.content.replace(regex1, `$2 style="display:block;max-width:100%!important;height:auto!important;"$3`);
            this.showDetail = true;
        },
    },
    components: {
        VueUeditorWrap
    }
};
</script>

<style scoped>
.continer{padding-bottom:62px;}
.editor{height:400px; color:#333; line-height:1.42;}
.dn{display:none;}
.w_100{width:100%;}

.ml_10{margin-left:10px;}
.txt_r {text-align: right;}
.topbox {margin-bottom: 20px;}
.detailTable{font-size:16px;}
.detailTable td{padding:4px 8px;}
.detailTable .tr{text-align:right;}
.topbox{display:flex; align-items:center; justify-content: space-between;}
.topbox .el-form-item{margin-bottom:0;}

.quill_pb{padding-bottom:46px;}
.tagipt{display:inline-block; padding:0 15px; width: 114px; height:30px; line-height:1; border:1px solid #DCDFE6; border-radius:16px; color:#606266;}
.tag_txt{display:inline-block; margin-right:10px; height:30px; line-height:30px; padding:0 10px 0 15px; border-radius:16px; background:#409EFF; color:#fff;}
.tag_txt>*{vertical-align:middle;}
.tag_txt .close{cursor:pointer; padding-left:10px;}
.form-img{max-width:140px; max-height:140px;}

.word{padding:20px;}
.word>div{line-height: 40px; font-size: 16px; margin-top: 10px;}
</style>
